-
-
Notifications
You must be signed in to change notification settings - Fork 142
feat(database): add Count query builder and statement
#1174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@brendt Not sure if the query builder is marked as experimental or not, but IF we can have breaking changes on this |
Count Query Builder and StatementCount query builder and statement
|
Nice PR! As I mentioned on the Discord, I think I'd keep this under the select query builder. I'd envision this being something like: public function count(string $column = '*'): int
{
// Do the work, accept where statements.
}I'd add to the query builder a count helper that could be something like: public function count(string $column = '*'): int
{
return $this->select()->count($column);
}Columns previously specified can simply be ignored, I'd think. That's just IMO. |
|
Oh, interesting approach, I admit I also thought it would have be part of the That's why I like the separation, as it clearly indicates that
The whole ORM is experimental, so we're fine 👍
This is something we can solve with traits in the long run. I prefer to keep the implementations apart for a while though, to make sure we don't over-abstract up front. Especially in an experimental component like the ORM. |
src/Tempest/Database/src/Builder/QueryBuilders/CountQueryBuilder.php
Outdated
Show resolved
Hide resolved
I think you misunderstood me, @brendt, because I don't think trait would solve the problem I described 🤔. What I mentioned is not the fact that I wonder if query('books')
->where('title = ?', $title)
->select()
->groupBy(...)
->all();Or this: query('books')
->select()
->where('title = ?', $title)
->groupBy(...)
->all();But not this (since query('books')
->groupBy(...)
->select()
->where('title = ?', $title)
->all();That way we could reuse the built query, like: $query = query('books')
->where('title = ?', $title);
$count = $query->count()->execute();
$results = $query->all(); |
e04156f to
cea03e1
Compare
|
Gotcha, yeah I misunderstood entirely 😁 It's an interesting idea, but I suggest we discuss it in another issue, it's out of scope for this PR :) |
|
Honestly, since we only return an Duh. Let me remove that, it's useless. |
|
@brendt Added support for distinct count and removed the useless alias. Here's how to do it: query('books')
->count('title')
->distinct()
->execute();If you do the following, an exception will be thrown, since you must specify a column for distinct: query('books')
->count() // or ->count('*')
->distinct()
->execute(); |
|
Awesome! Are you up for adding the docs for this as well? |
|
See #1179 for documentation followup |
Sure thing, but I may not be able to get around to it until after may 1st. |
This pull request introduces a new
CountQueryBuilderto the Tempest database library, enabling efficient SQLCOUNTqueries with fluent condition chaining. It includes the implementation of the builder, integration with existing query builders, and comprehensive unit and integration tests.New Feature:
CountQueryBuilderCountQueryBuilderclass to support building SQLCOUNTqueries with methods for chaining conditions (where,andWhere,orWhere, etc.) and specifying columns. It integrates withModelDefinitionandTableDefinitionfor resolving table and field details. (src/Tempest/Database/src/Builder/QueryBuilders/CountQueryBuilder.php)count()method in theQueryBuilderclass to create instances ofCountQueryBuilder, allowing seamless integration with existing query workflows. (src/Tempest/Database/src/Builder/QueryBuilders/QueryBuilder.php)Supporting Query Statement
CountStatementclass, which compiles the SQL forCOUNTqueries, including support for optionalWHEREclauses and column specification. (src/Tempest/Database/src/QueryStatements/CountStatement.php)TODOs:
Add support for(As brought up on discord, this makes more sense on the SELECT query builder only)GROUP BYandHAVINGAdd support for aliasing the(There's no point aliasing the count if the only thing returned is anCOUNTint)DISTINCTCOUNTin a->selectCaveats (?)
The problem with Tempest's approach of defining the "query type" (
select/update/delete) before building the rest of the query can make it difficult to reuse the query conditions.For example - say you need to implement pagination from a filterable query - you'll need to repeat yourself in order to get the results and also a total count:
Closes #1164